home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbuts.zip / TWISTER.C < prev   
C/C++ Source or Header  |  1991-05-16  |  6KB  |  176 lines

  1. /******************************************************************
  2.  * 
  3.  * Program : Twister.c
  4.  * Purpose : Create twisted objects for the DKB raytracer
  5.  * Created : 12/05/90
  6.  * By      : Drew Wells [CIS 73767,1244]
  7.  * Files   : Twister.c
  8.  * Compiler: Microsoft C 6.00a
  9.  * Model   : Tiny
  10.  * Comments: This simple program used to create the floating twisters in
  11.  *          the image "Not a Trace of Reality" a.k.a. Ntreal.gif.
  12.  *          It should be compatible with any ANSI C compiler, and is 
  13.  *          easily modifiable to create more complex shapes by adding
  14.  *          code to change the x and z axis rotation and step.
  15.  *
  16.  *
  17.  * Copyright 1990 Drew Wells
  18.  * This copyrighted code is released for non-commercial use only.
  19.  * It may not be sold or used a part of a commercial package.
  20.  * This code may be modified and distributed provided it is done at no
  21.  * cost to the user.
  22.  * Please comment and maintain version list if you modify this code.
  23.  ******************************************************************/
  24. /* Version
  25.  *   0.01 12/05/90 DW Created program.   
  26.  *   1.00 12/26/90 DW Added user input.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. void show_title(void); 
  35. void get_parameters(void);
  36. void write_header(char *filename, char *union_name,  char *objname);
  37. void write_quadric(char *objname);
  38. void write_piece(char *objname,double xpos, double ypos, double zpos,
  39.                                double xrot, double yrot, double zrot);
  40. void write_end(char *filename, char *union_name);
  41. void err_exit(char *message);
  42.  
  43. FILE *outfile;
  44. char object[80],filename[80],union_name[80],workstr[256];
  45. double length, numtwist, ystep, rstep,xrot,yrot,zrot,x,y,z,numdeg;
  46. long numpieces;
  47.  
  48.     
  49.  
  50. main()
  51. {  
  52.    show_title();
  53.    
  54. /* Get twister values from user */
  55.     get_parameters();
  56.  
  57. /* Open file for output  and write header */
  58.     printf("\n\nCreating DKB data file %s...\n",filename);
  59.     if ((outfile = fopen(filename,"w")) == NULL)
  60.      err_exit("Opening file.");
  61.     
  62.     write_header(filename,union_name, object);
  63.     
  64. /* Create twister and write each piece to file */ 
  65.     for(y=0.0;y<=length;y+=ystep){
  66.       write_piece(object,x,y,z,xrot,yrot,zrot);
  67.       yrot += rstep;
  68.      if(yrot>=360.0)
  69.          yrot = yrot - 360.0;
  70.      }
  71.  
  72. /* Write object end and close file */
  73.     write_end(filename,union_name);
  74.     fclose(outfile);
  75.     printf("\nDKB data file %s created.\n",filename);
  76.     return(0);
  77. }
  78.  
  79.  
  80. void show_title(void)
  81.    printf("\n\n\n\n");
  82.    printf("___________________________________________________________________\n\n");
  83.    printf("[ TWISTER V1.00 ]\n");
  84.    printf("This program creates a DKB raytracer data file of a twisted object.\n"); 
  85.    printf("See TWISTER.doc for more details.\n");
  86.    printf("- DW 12/26/90\n");
  87.    printf("___________________________________________________________________\n");
  88. }
  89. void get_parameters(void)
  90. /* Get twister values from user */    
  91.     
  92.     printf("Twister filename? [twist.dat]: ");
  93.     gets(workstr);
  94.     strcpy(filename,workstr);
  95.     
  96.     printf("Union name? [Macaroni]: ");
  97.     gets(workstr);
  98.     strcpy(union_name,workstr);
  99.     
  100.     printf("Quadric name? [Part]: ");
  101.     gets(workstr);
  102.     strcpy(object,workstr);
  103.     
  104.     
  105.     printf("Length of twister? [20.0]: ");
  106.     gets(workstr);
  107.     sscanf (workstr, "%lf", &length);
  108.     
  109.     printf("Number of pieces for twister? [50]: ");
  110.     gets(workstr);
  111.     numpieces = atoi(workstr);
  112.     
  113.     printf("Number of twists? [1.0]: ");
  114.     gets(workstr);
  115.     sscanf (workstr, "%lf", &numtwist);
  116.  
  117.  /* Set up default values */
  118.     if(filename[0]=='\0') strcpy(filename,"twist.dat");
  119.     if(union_name[0]=='\0') strcpy(union_name,"Macaroni");
  120.     if(object[0]=='\0') strcpy(object,"Part");
  121.     if(length==0.0) length = 20.0;
  122.     if(numpieces==0) numpieces = 50;
  123.     if(numtwist==0.0) numtwist = 1.0;
  124.     
  125.     xrot = yrot = zrot = x = y = z = 0.0;
  126.     ystep = length/(double)(numpieces-1);
  127.     rstep = (360.0 * numtwist)/(length/ystep); 
  128. }
  129.  
  130. void write_header(char *filename,  char *union_name, char *objname)
  131.   fprintf(outfile,"{ File : %s  Union Name : %s }\n",filename,union_name);
  132.   fprintf(outfile,"{ This data file created by TWISTER for the DKB ray tracer. }\n");
  133.   fprintf(outfile,"{ NOTE: Change quadric '%s' to change the look of '%s' }\n",objname,union_name); 
  134.   fprintf(outfile,"{ See TWISTER docs for more details.}\n\n");
  135.   write_quadric(objname);
  136.   fprintf(outfile,"\n{ Twister %s Length=%.3lf Number pieces=%ld Number twists=%.3lf }\n",union_name,length,numpieces,numtwist);
  137.   fprintf(outfile,"DECLARE %s = OBJECT \n UNION\n",union_name);
  138. }
  139.  
  140. void write_quadric(char *objname)
  141.   /* Write a sample quadric to file for ease of use */
  142.   fprintf(outfile,"DECLARE %s = QUADRIC Sphere ",objname);
  143.   fprintf(outfile,"SCALE < %.2lf 1.0 1.0 > END_QUADRIC\n",length/4.0);
  144. }  
  145. void write_piece(char *objname,double xpos, double ypos, double zpos,
  146.                             double xrot, double yrot, double zrot)
  147. {
  148.     char tofile[256];
  149.     
  150.     sprintf(tofile,"   QUADRIC %s\n   ROTATE <%.3lf %.3lf %.3lf> TRANSLATE <%.3lf %.3lf %.3lf>\n   END_QUADRIC\n",
  151.                 objname,xrot,yrot,zrot,xpos,ypos,zpos);
  152.  
  153.     if( fputs(tofile,outfile) )
  154.      err_exit("Writing string to file.");
  155.     
  156. }
  157.   
  158. void write_end(char *filename, char *union_name)
  159. {
  160.  fprintf(outfile," END_UNION\n TEXTURE COLOR RED 0.9 GREEN 0.1 BLUE 0.1 END_TEXTURE \nEND_OBJECT\n");
  161.  fprintf(outfile,"{ End_File : %s  End_Union Name : %s }\n",filename,union_name);
  162. }
  163.  
  164. void err_exit(char *message)
  165. {
  166.     puts("\n\nERROR! \a");
  167.     puts(message);
  168.     puts("- Exiting program\n[Press CR]");
  169.     getchar();
  170.     exit(1);
  171. }
  172.